<?php
class Zenddn_System_Acl{
    
    protected $_acl;
    protected $_role;
    
    public function __construct($aclInfo = null){
    	if(!empty($aclInfo)){
    	    $acl = new Zend_Acl();
        
            $role = $aclInfo['role'];
            $privileges = $aclInfo['privileges'];
             
            $acl->addRole($role);
            $acl->allow($role, null, $privileges);
            
            $this->_acl = $acl;
            $this->_role = $role;
    	}
    }
    
    public function isAllowed($params = null){
        $privilege = $params['module'] . ":" . $params['controller'] . ":" . $params['action'];
        
        if($this->_acl->isAllowed($this->_role, null, $privilege)){
            return true;
        }
        
        return false;
    }
    
    public function createPrivileges($option = null){
    	$ns = new Zend_Session_Namespace('info');
        $info = $ns->user;
        $group_id = $info->user_group;
        
        $db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_ASSOC);
        
        $select = $db->select()
                    ->from(array('ugp' => 'user_group_privileges'), array('user_group', 'privilege_id'))
                    ->join(array('p' => 'privileges'), "ugp.privilege_id = p.id")
                    ->where("ugp.status = 1")
                    ->where("ugp.user_group = ?", $group_id, 'INTEGER');
        
        $result = $db->fetchAll($select);
        
        $privilege = array();
        
        if(!empty($result)){
            foreach($result as $value){
                $privilege[] = $value['module'] . ":" . $value['controller'] . ":" . $value['action'];
            }
        }
        
        $ns->acl['privileges'] = $privilege;
    }
    
    public function createRole($option = null){
    	$ns = new Zend_Session_Namespace('info');
        $info = $ns->group;
        $group_name = $info->group_name;
        
        $ns->acl['role'] = $group_name;
    }
}